package org.jkuang.qstar.commons.jni;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;

public  class I2IMap extends Native {
	static AtomicInteger seq = new AtomicInteger(0);
	private int index = seq.getAndIncrement();

	public I2IMap(int part) {
		create(index, part);
	}

	public void put(long key, long value) {
		put(index, key, value);
	}

	public void remove(long key) {
		remove(index, key);
	}

	public long get(long key) {
		return get(index, key);
	}

	public int size() {
		return size(index);
	}

	public boolean readfile(String pathname) {
		File file = new File(pathname);
		if (file.exists() && file.length() > 1024 * 8) {
			return readfile(index, pathname);
		} else {
			return false;
		}
	}

	public boolean writefile(String pathname) {
		File file = new File(pathname);
		File dir = file.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return writefile(index, pathname);
	}

	public void destroy() {
		destroy(index);
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
		destroy();
	}

	private native void create(int index, int mode);

	private native void destroy(int index);

	private native void put(int index, long key, long value);

	private native long get(int index, long key);

	private native int size(int index);

	private native void remove(int index, long key);

	private native boolean readfile(int index, String filename);

	private native boolean writefile(int index, String filename);

}